package org.csu.mypetstore.web.servlets;

import org.csu.mypetstore.persistence.DBUtil;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;

public class UsernameIsExistServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String name = request.getParameter("username");
        String checekname = name+"123";
        System.out.println(name+"123");
        try{	//创建数据库的连接
            Connection connection = DBUtil.getConnection();
            //sql语句
            String sql ="SELECT * FROM account where userid= ? ";
            java.sql.PreparedStatement pstm = connection.prepareStatement(sql);
            pstm.setString(1, name);
            ResultSet rs = pstm.executeQuery();
            PrintWriter out = response.getWriter();
            Boolean b =(checekname.equals("123"));
            System.out.println(b);
            if(rs.next()||b||name.length()<5){
                System.out.println(rs.next()+"***************");
                System.out.println(name+"11111111111111111111123");
                out.print("Exist");
            }
            else{
                System.out.println(rs.next()+"+++++++++++++++++++++++++++");
                System.out.println(name+"11111111111111111111123");
                out.print("Not Exist");
            }
            out.flush();
            out.close();
        }catch(Exception e){
            e.printStackTrace();
        }

    }


}


